#include <cstdio>
#include <string>
#include <vector>

#include "Cengine.h"

#include "CGameObject.h"

const char* str1 = "abcdefg";
const char* str2 = "abfcdeg";

// test diff for text strings :)

void PrintDiff(int *c, const char* x, const char* y, int i, int j)
{
	int m = strlen(x);
	int n = strlen(y);

	if (i > 0 && j > 0 && x[i-1] == y[j-1])
	{
		PrintDiff(c, x, y, i-1, j-1);
		printf("  %c\n", x[i-1]);

	}
	else
	{
		if (j > 0 && (i == 0 || c[(n+1)*i + j-1] >= c[(n+1)*(i-1) + j]))
		{
			PrintDiff(c, x, y, i, j-1);
			printf("+ %c\n", y[j-1]);
		} else if (i > 0 && (j == 0 || c[(n+1)*i + j-1] < c[(n+1)*(i-1) + j]))
		{
			PrintDiff(c, x, y, i-1, j);
			printf("- %c\n", x[i-1]);
		}
	}
}

void LCSLength( const char* x, const char* y)
{
	int m = strlen(x);
	int n = strlen(y);

	int* C = new int[(m+1)*(n+1)];

	for (int i = 0; i < m+1; ++i)
		C[(n+1)*i] = 0;	// C[i,0] = 0;

	for (int j = 0; j < n+1; ++j)
		C[j] = 0;	// C[0,j] = 0;

	
	
	for (int i = 1; i < m+1; ++i)
		for (int j = 1; j < n+1; ++j)
		{
			if (x[i-1] == y[j-1])
				C[(n+1)*i + j] = C[(n+1)*(i-1) + j-1] + 1;
			else
				C[(n+1)*i + j] = std::max(C[(n+1)*i + j-1], C[(n+1)*(i-1) + j]);
		}
	
	
	printf("print matrix C:\n");
	printf("    ");
	for (int i = 0; i < n; ++i)
		printf("%c ", y[i]);
	printf("\n");

	for (int i = 0; i < m+1; ++i)
	{
		if (i == 0)
			printf("  ");
		else
			printf("%c ", x[i-1]);

		for (int j = 0; j < n+1; ++j)
			printf("%d ", C[(n+1)*i + j]);
		printf("\n");
	}

	PrintDiff(C, x, y, m, n);

	delete[] C;
}

int main(int argc, char** argv)
{
#if 0
	LCSLength(str1, str2);
	
	std::vector<int> a(100);
	a.reserve(100000);
	a.erase(a.begin(), a.end());
	printf("a = %ld\n", a.capacity());
#else
	try {
		Cengine engine(800, 600);
		engine.Init();
		engine.Run();
	} catch(std::exception& e) {
		fprintf(stdout, "%s: \"%s\"\n", e.what(), SDL_GetError());
	}
#endif

	return 0;
}